import React,{useState} from 'react'
import { register } from '../api/user'
import { message,Button, Form, Input } from 'antd';

const Register = () => {
    const [loading,setLoading] = useState(false);
    const handleRegister=(values)=>{
        setLoading(true)
        register(values)
            .then(res=>{
                console.log(res)
                if(res.code === '200'){
                    message.success("注册成功！！！")
                }
            })
            .finally(()=>{
                setLoading(false)
            })
    }
    return (
        <div>
            <Form
                name="basic"
                layout='vertical'
                style={{margin:'50px auto',width: 400}}
                onFinish={handleRegister}
                autoComplete="off"
            >
                <Form.Item
                    label="用户名"
                    name="userName"
                    rules={[{ required: true, message: '请输入用户名!' }]}
                >
                <Input />
                </Form.Item>

                <Form.Item
                    label="密码"
                    name="password"
                    hasFeedback
                    rules={[{ required: true, message: '请输入密码!' }]}
                >
                <Input.Password />
                </Form.Item>

                <Form.Item
                    label="确认密码"
                    name="confirm"
                    dependencies={['password']}
                    hasFeedback
                    rules={[
                    {
                        required: true,
                        message: '请确认密码!',
                    },
                    ({ getFieldValue }) => ({
                        validator(_, value) {
                        if (!value || getFieldValue('password') === value) {
                            return Promise.resolve();
                        }
                        return Promise.reject(new Error('两次密码不同!'));
                        },
                    }),
                    ]}
                >
                    <Input.Password />
                </Form.Item>

                <Form.Item>
                    <Button block shape='round' type="primary" htmlType="注册" loading={loading}>
                        注册
                    </Button>
                </Form.Item>
            </Form>
        </div>

    ) 
}

export default Register